package taskit.datastructures;

import java.io.Serializable;
import java.util.LinkedList;

import org.codehaus.jackson.annotate.JsonTypeInfo;

import taskit.test.Log;

//@author A0087072A
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public class Deadline extends Task implements Serializable,
		Comparable<Deadline> {

	private Date endDate;

	/**
	 * This empty constructor is required for the JSON parser
	 */
	public Deadline() {
	}

	//@author A0098733R
	public Deadline(String name, Date endDate) {
		super(name);
		this.endDate = endDate;
	}

	public Deadline(String name, Date endDate, LinkedList<String> tags) {
		super(name, tags);
		this.endDate = endDate;
	}
	
	public static Deadline createDeadline(String name, Date endDate) throws Exception {
		if (endDate.compareTo(Date.currentDate()) < 0) {
			throw new Exception(
					"You may not create a task that falls before now.");
		}
		return new Deadline(name, endDate);
	}

	public static Deadline createDeadline(String name, Date endDate, LinkedList<String> tags) throws Exception {
		if (endDate.compareTo(Date.currentDate()) < 0) {
			throw new Exception(
					"You may not create a task that falls before now.");
		}
		return new Deadline(name, endDate, tags);
	}

	public Date getEndDate() {
		return endDate;
	}
	
	

	/**
	 * Sets the ending date for this instance of Deadline
	 * 
	 * @param newEndDate
	 *            new date to add in
	 * @return 0 if successful, 1 on failure Last modified: 13th March 2014 by
	 *         joe
	 */
	//@author A0087072A
	public int setEndDate(Date newEndDate) {
		try {
			this.endDate = newEndDate;
		} catch (Exception anyEx) {
			anyEx.printStackTrace();
			return 1;
		}
		return 0;
	}

	//@author A0098733R
	@Override
	public int compareTo(Deadline other) {
		int result = this.getEndDate().compareTo(other.getEndDate());
		if (result == 0) {
			result = this.getName().toLowerCase()
					.compareTo(other.getName().toLowerCase());
			if (result == 0) {
				result = this.getClass().getName()
						.compareTo(other.getClass().getName());
				if (result == 0) {
					if (this.getClass().equals("Schedule")) {
						result = ((Schedule) this).getStartDate().compareTo(
								((Schedule) other).getStartDate());
					}
				}
			}
		}
		return result;
	}

	public boolean equals(Deadline deadline) {
		return this.equalsName(deadline)
				&& ((deadline.endDate == null) ? this.endDate == null
						: deadline.endDate.equals(this.endDate))
				&& ((deadline instanceof Schedule) ? (this instanceof Schedule)
						&& ((((Schedule) deadline).getStartDate() == null) ? ((Schedule) this)
								.getStartDate() == null : ((Schedule) deadline)
								.getStartDate().equals(
										((Schedule) this).getStartDate()))
						: !(this instanceof Schedule));

	}
}
